package com.realgodo.site.service.application;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.application.vote.Vote;
import com.realgodo.site.entity.application.vote.VoteRecord;
import com.realgodo.site.entity.space.Space;

/**
 * @author bijiqiang E-mail: bijiqiang@gmail.com
 * @version 创建时间：2009-12-13下午09:47:32
 * @since jdk1.5
 * @version 0.1
 */
@Service("voteRecordManager")
@Transactional
public class VoteRecordManager extends DefaultEntityManager<VoteRecord, Long>{
	@Autowired
	protected SessionFactory sessionFactory;
	/**
	 * 获取我参与的投票
	 * @param list
	 * @param space
	 * @return
	 */
	public Page<VoteRecord> getMyJoinVoteRecords(Page<VoteRecord> list, Space space) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(VoteRecord.class);
		
		criteria.add(Restrictions.eq("voter.id", space.getId()));
		criteria.addOrder(Order.desc("voteTime"));
		
		return super.getEntityDao().findByCriteria(list, criteria);
	}
}
